package com.newsproject.mapper;


import com.newsproject.pojo.VerificationCode;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.Optional;

@Mapper
public interface CodeMapper {
    @Insert("insert into verification_code (code , email , expiration_time, created_time) values (#{code}, #{email}, #{expirationTime}, #{createTime})")
    void insertVerificationCode(VerificationCode verificationCode);

    @Delete("delete  from verification_code where expiration_time  <   current_timestamp ")
    void deleteExpiredVerificationCode();

    @Select("select  code  , email , created_time as createTime, expiration_time as expirationTime  from verification_code where email = #{email} and expiration_time > current_timestamp order by created_time desc limit 1")
    Optional<VerificationCode>  getVerificationCodeByEmail(String email);
}
